<%-- 
    Document   : addOrEditGoods
    Created on : Oct 27, 2011, 7:52:54 AM
    Author     : Admin
--%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="manufacturerBean.ManufacturerHome"%>
<%@page import="manufacturerBean.Manufacturer"%>
<%@page import="categoryBean.Category"%>
<%@page import="categoryBean.CategoryHome"%>
<%@page import="goodsBean.Goods"%>
<%@page import="goodsBean.GoodsHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<%@page import="DBConnection.ATeamObject"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page errorPage = "Error.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Добавление товара</title>
        <%  InitialContext initialContext;
            GoodsHome goodsHome;
            Goods goods;     
            Object homeObject;
            
            CategoryHome categoryHome;
            Category category;
            Object homeObject1;
            
            Manufacturer manufacturer;
            ManufacturerHome manufacturerHome;
            Object homeObject2;
                 
            initialContext = new InitialContext();
            homeObject = initialContext.lookup("GoodsBean");
            goodsHome = (GoodsHome) PortableRemoteObject.narrow(homeObject,
                    GoodsHome.class);

            homeObject1 = initialContext.lookup("CategoryBean");
            categoryHome = (CategoryHome) PortableRemoteObject.narrow(
                    homeObject1, CategoryHome.class);

            homeObject2 = initialContext.lookup("ManufacturerBean");
            manufacturerHome = (ManufacturerHome) PortableRemoteObject.
                    narrow(homeObject2, ManufacturerHome.class);
        %>
    </head>
    <body background="1.jpg" bgproperties="fixed">
        <h4><a href="index.jsp">Главная</a> / 
            <a href="addOrEditGoods.jsp">Добавление товара</a> </h4> 
        <h1>Добавление товара</h1>
        <%  if (request.getParameter("category") != null){
                ATeamObject atoForNewGood = new ATeamObject();
                atoForNewGood.setName(request.getParameter("name"));                
                atoForNewGood.setObjectDescription(request.getParameter(
                        "description"));
                
                category = categoryHome.findByPrimaryKey(Integer.parseInt(
                        request.getParameter("category")));
                atoForNewGood.setParentId(category.getObjectId());
                
                if (!request.getParameter("manufacturer").equals("0")) {
                    manufacturer = manufacturerHome.findByPrimaryKey(Integer.
                            parseInt(request.getParameter("manufacturer")));
                    atoForNewGood.setManifacturerId(manufacturer.getObjectId());
                }
                    
                atoForNewGood.setPrice(Integer.parseInt(request.getParameter(
                        "price")));
                atoForNewGood.setNumber(Integer.parseInt(request.getParameter(
                        "value")));
                
                goods = goodsHome.create(atoForNewGood);
                
                response.sendRedirect("index.jsp");                
            }
        %>
        <form name="addGoods" action="addOrEditGoods.jsp">
            <p>
                Введите название
                <input type="text" name="name" value="" size="50" />
            </p>
            <p>    
                Введите описание
                <input type="text" name="description" value="" size="50" />
            </p>    
            <p>
                Выберите категорию
                <select name="category">
                <%  Collection allCategories = categoryHome.findAll();
                    Iterator iter = allCategories.iterator();
                    while(iter.hasNext()){
                        category = (Category)iter.next();
                %>
                        <option value="<%= category.getObjectId() %>"><%= category.getName() %></option>
                <%
                    }
                %>
                </select>
            </p>    
            <p>
                Выберите производителя
                <select name="manufacturer">
                    <option value="0">Другой</option>
                    <%  Collection allManufacturers = manufacturerHome.findAll();
                        iter = allManufacturers.iterator();
                        while(iter.hasNext()){
                            manufacturer = (Manufacturer)iter.next();
                    %>
                    <option value="<%= manufacturer.getObjectId() %>"><%= manufacturer.getName() %></option>
                <%
                        }
                %>
                </select>
            </p>
            <p> Введите цену
                <input type="text" name="price" value="" size="25" />
            </p>
            <p> Введите количество
                <input type="text" name="value" value="" size="25" />
            </p>
            <p>
                <input type="submit" value="Добавить" name="addToDB" />
            </p>
        </form>
    </body>
</html>
